package com.websocket.action;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.websocket.pojo.User;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
	}
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User currentUser = getUser(request);
		if(currentUser.getUsername()==null || currentUser.getPassword()==null || currentUser.getPassword() == "" || currentUser.getUsername() == "") {
			request.setAttribute("msg", "错误的用户名或者密码！");
			response.sendRedirect("login.jsp");
			return;
		}
		request.getSession().setAttribute("user", currentUser);
		request.getRequestDispatcher("index.jsp").forward(request, response);
		
	}
	
	private User getUser(HttpServletRequest request) {
		User u = new User();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String jsessionId = request.getSession().getId();
		u.setJsessionId(jsessionId);
		u.setPassword(password);
		u.setUsername(username);
		return u;
	}
}
